package cn.com.api.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_match_vote;
import cn.com.api.service.ApiU_match_voteService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_match_vote/")
public class ApiU_match_voteController {

	@Autowired
	private ApiU_match_voteService apiU_match_voteService;

	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiU_match_voteController.class);

	/**
	 * 根据赛事ID报名
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("insertU_match-vote")
	public void insertU_match_vote(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {

			String works_id = request.getParameter("works_id");
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String vote_time = sd.format(new Date());
			ApiU_match_vote u_match_vote = new ApiU_match_vote();
			u_match_vote.setVote_time(vote_time);
			u_match_vote.setMatch_works_id(works_id);
			u_match_vote.setUser_id(user_id);
			u_match_vote.setExt1("");
			u_match_vote.setExt2("");
			u_match_vote.setExt3("");

			if (checkService.checkToken(user_id, token, map)) {

				apiU_match_voteService.insertU_match_vote(u_match_vote);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "投票成功！");
			}

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

}
